package icu.bughub.app.app.ui.components

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.google.accompanist.insets.LocalWindowInsets
import com.google.accompanist.systemuicontroller.rememberSystemUiController
import icu.bughub.app.app.ui.theme.Blue200
import icu.bughub.app.app.ui.theme.Blue700

/**
 * 顶部导航栏
 * @param modifier 修饰符
 * @param statusBarHeight 状态栏高度
 * @param content 标题栏内容
 * @author zw
 * @version 1.0
 * @since 2025年06月05日19:23:43
 */

val appBarHeight = 56.dp // 定义 appBar 的高度

@Composable
fun TopAppBar(
    modifier: Modifier = Modifier,
    content: @Composable () -> Unit
) {

    val systemUiController = rememberSystemUiController()

    LaunchedEffect(key1 = Unit) {
        systemUiController.setStatusBarColor(
            color = Color.Transparent,
        )
    }

    // 根据屏幕密度计算 statusBar 的高度
    val statusBarHeightDp = with(LocalDensity.current) {
        //statusBarHeight.toDp()
        LocalWindowInsets.current.statusBars.top.toDp()
    }

    Row(
        modifier = Modifier
            .fillMaxWidth()
            .background(
                Brush.linearGradient(
                    listOf(
                        Blue700,
                        Blue200
                    )
                )
            )
            .height(appBarHeight + statusBarHeightDp)
            .padding(top = statusBarHeightDp)
            .then(modifier), //合并传进来的修饰符
        horizontalArrangement = Arrangement.Center,
        verticalAlignment = Alignment.CenterVertically
    ) {
        content()
    }
}

@Preview
@Composable
fun TopAppBarPreview() {

}